<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.1.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.1.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=6.1.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=6.1.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.1.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.1.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="更新日志    2018-04-14: 创建  2018-04-15：增加字符测试   Bash条件测试条件测试种类整数测试（双目操作符）   符号 数学符号 含义     -eq = 相等测试   -ne ≠ 不等测试   -gt &amp;gt; 大于测试   -ge ≥ 大于等于   -lt &amp;lt; 小于测试   -le ≤ 小于等于测试    字符串测试（双目单目都有）   符号 含义">
<meta name="keywords" content="Linux,Shell,bash,Branch_Structure">
<meta property="og:type" content="article">
<meta property="og:title" content="Bash脚本编程——分支结构">
<meta property="og:url" content="http://yoursite.com/bash-branch-structure/index.html">
<meta property="og:site_name" content="胖勇の小窝">
<meta property="og:description" content="更新日志    2018-04-14: 创建  2018-04-15：增加字符测试   Bash条件测试条件测试种类整数测试（双目操作符）   符号 数学符号 含义     -eq = 相等测试   -ne ≠ 不等测试   -gt &amp;gt; 大于测试   -ge ≥ 大于等于   -lt &amp;lt; 小于测试   -le ≤ 小于等于测试    字符串测试（双目单目都有）   符号 含义">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-05-18T10:34:58.006Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Bash脚本编程——分支结构">
<meta name="twitter:description" content="更新日志    2018-04-14: 创建  2018-04-15：增加字符测试   Bash条件测试条件测试种类整数测试（双目操作符）   符号 数学符号 含义     -eq = 相等测试   -ne ≠ 不等测试   -gt &amp;gt; 大于测试   -ge ≥ 大于等于   -lt &amp;lt; 小于测试   -le ≤ 小于等于测试    字符串测试（双目单目都有）   符号 含义">






  <link rel="canonical" href="http://yoursite.com/bash-branch-structure/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Bash脚本编程——分支结构 | 胖勇の小窝</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">胖勇の小窝</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>




<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
          
  
  <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
</li>

      
        
        
          
  
  <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
</li>

      
        
        
          
  
  <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
</li>

      
        
        
          
  
  <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
</li>

      
        
        
          
  
  <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
</li>

      

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/bash-branch-structure/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="LiuGaoyong">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://p7b7this6.bkt.clouddn.com/18-6-3/47273717.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="胖勇の小窝">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Bash脚本编程——分支结构
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
                
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-15T17:16:26+08:00">2018-04-15</time>
            

            
            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Liunx运维/" itemprop="url" rel="index"><span itemprop="name">Liunx运维</span></a></span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Liunx运维/0-Bash编程/" itemprop="url" rel="index"><span itemprop="name">0 Bash编程</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <font size="4" face="黑体">更新日志</font> 

<blockquote>
<p>2018-04-14: 创建<br>  2018-04-15：增加字符测试</p>
</blockquote>
<hr>
<h3 id="Bash条件测试"><a href="#Bash条件测试" class="headerlink" title="Bash条件测试"></a>Bash条件测试</h3><h4 id="条件测试种类"><a href="#条件测试种类" class="headerlink" title="条件测试种类"></a>条件测试种类</h4><h5 id="整数测试（双目操作符）"><a href="#整数测试（双目操作符）" class="headerlink" title="整数测试（双目操作符）"></a>整数测试（双目操作符）</h5><table>
<thead>
<tr>
<th>符号</th>
<th>数学符号</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>-eq</td>
<td>=</td>
<td>相等测试</td>
</tr>
<tr>
<td>-ne</td>
<td>≠</td>
<td>不等测试</td>
</tr>
<tr>
<td>-gt</td>
<td>&gt;</td>
<td>大于测试</td>
</tr>
<tr>
<td>-ge</td>
<td>≥</td>
<td>大于等于</td>
</tr>
<tr>
<td>-lt</td>
<td>&lt;</td>
<td>小于测试</td>
</tr>
<tr>
<td>-le</td>
<td>≤</td>
<td>小于等于测试</td>
</tr>
</tbody>
</table>
<h5 id="字符串测试（双目单目都有）"><a href="#字符串测试（双目单目都有）" class="headerlink" title="字符串测试（双目单目都有）"></a>字符串测试（双目单目都有）</h5><table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>==</td>
<td>判断两个字符串是否相等</td>
</tr>
<tr>
<td>!=</td>
<td>判断两个字符串是否相等</td>
</tr>
<tr>
<td>&lt;</td>
<td>按照字符ASCII数字从前向后比较，数值大的大</td>
</tr>
<tr>
<td>></td>
<td></td>
</tr>
<tr>
<td>-n</td>
<td>判断字符串是不是为空串</td>
</tr>
<tr>
<td>-z</td>
<td>判断字符串是不是为空串</td>
</tr>
</tbody>
</table>
<h5 id="文件测试（单目操作符）"><a href="#文件测试（单目操作符）" class="headerlink" title="文件测试（单目操作符）"></a>文件测试（单目操作符）</h5><table>
<thead>
<tr>
<th>符号</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr>
<td>-e $FILENAME</td>
<td>是否存在</td>
</tr>
<tr>
<td>-f $FILENAME</td>
<td>是否是文件</td>
</tr>
<tr>
<td>-d $FILENAME</td>
<td>是不是目录</td>
</tr>
<tr>
<td>-r $FILENAME</td>
<td>有没有读权限</td>
</tr>
<tr>
<td>-w $FILENAME</td>
<td>有没有写权限</td>
</tr>
<tr>
<td>-x $FILENAME</td>
<td>有没有执行权限</td>
</tr>
</tbody>
</table>
<h4 id="条件测试表达式"><a href="#条件测试表达式" class="headerlink" title="条件测试表达式"></a>条件测试表达式</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[ expression ]</span><br><span class="line"></span><br><span class="line">[[ expression ]]</span><br><span class="line"></span><br><span class="line"><span class="built_in">test</span> expression  <span class="comment"># test 是一个Bash命令</span></span><br></pre></td></tr></table></figure>
<h4 id="算术运算的实现方式"><a href="#算术运算的实现方式" class="headerlink" title="算术运算的实现方式"></a>算术运算的实现方式</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">let</span> <span class="variable">$A</span>+<span class="variable">$B</span></span><br><span class="line"></span><br><span class="line">$[ <span class="variable">$A</span>+<span class="variable">$B</span> ]</span><br><span class="line"></span><br><span class="line">$(( <span class="variable">$A</span>+<span class="variable">$B</span> ))</span><br><span class="line"></span><br><span class="line">expr ` <span class="variable">$A</span> + <span class="variable">$B</span> `   	<span class="comment"># 注：表达式中各操作数及运算符之间要有空格</span></span><br><span class="line">                        <span class="comment"># 而且要使用命令引用</span></span><br></pre></td></tr></table></figure>
<h3 id="分支结构（if语句）"><a href="#分支结构（if语句）" class="headerlink" title="分支结构（if语句）"></a>分支结构（if语句）</h3><h4 id="单分支的if语句"><a href="#单分支的if语句" class="headerlink" title="单分支的if语句"></a>单分支的if语句</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件判断表达式; <span class="keyword">then</span></span><br><span class="line">  expression1</span><br><span class="line">  expression2</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="双分支的if语句"><a href="#双分支的if语句" class="headerlink" title="双分支的if语句"></a>双分支的if语句</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件判断表达式; <span class="keyword">then</span></span><br><span class="line">  expression1</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  expression2</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="多分支的if语句"><a href="#多分支的if语句" class="headerlink" title="多分支的if语句"></a>多分支的if语句</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件判断表达式1; <span class="keyword">then</span></span><br><span class="line">  expression1</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">elif</span> 条件判断表达式2; <span class="keyword">then</span></span><br><span class="line">  expression2</span><br><span class="line">  ...</span><br><span class="line">  </span><br><span class="line">... ...</span><br><span class="line"></span><br><span class="line"><span class="keyword">elif</span> 条件判断表达式4; <span class="keyword">then</span></span><br><span class="line">  expression4</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  expression5</span><br><span class="line">  ...</span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h3 id="实例练习"><a href="#实例练习" class="headerlink" title="实例练习"></a>实例练习</h3><h4 id="给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；"><a href="#给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；" class="headerlink" title="给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；"></a>给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line">USER_UID=$(id -u <span class="variable">$1</span>)  <span class="comment"># $1为脚本所带的第一个参数，类似的$2,$3...</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$USER_UID</span> -eq 0 ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"This is administration."</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"This is a commen user."</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；"><a href="#判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；" class="headerlink" title="判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；"></a>判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line">BASH_USER_NUMBER=$( grep <span class="string">"bash$"</span> /etc/passwd | wc -l )</span><br><span class="line"><span class="keyword">if</span> [ $? -eq 0 ]; <span class="keyword">then</span>  <span class="comment"># $？为其前一行命令的执行状态返回值（ 0=True, 1...=False ）</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"There are <span class="variable">$BASH_USER_NUMBER</span> user(s) which use(s) bash."</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"There aren't any users which use bash."</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#	命令执行结果 ≠ 命令执行状态</span></span><br><span class="line"><span class="comment">#		1.  A=`ls`	 # 引用执行结果</span></span><br><span class="line"><span class="comment">#		2.  ls; A=$? # 引用执行状态</span></span><br></pre></td></tr></table></figure>
<h4 id="给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。"><a href="#给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。" class="headerlink" title="给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。"></a>给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line">BLANK_LINE_NUMBER=$( grep <span class="string">"^$"</span> <span class="variable">$1</span> | wc -l )</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$BLANK_LINE_NUMBER</span> -ne 0 ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"There are <span class="variable">$BLANK_LINE_NUMBER</span> blank line(s)."</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"There aren't any blank lines."</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。"><a href="#判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。" class="headerlink" title="判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。"></a>判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line">USER_UID=`id -u <span class="variable">$1</span>`</span><br><span class="line">GROUP_ID=`id -g <span class="variable">$1</span>`</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$USER_UID</span> -eq <span class="variable">$GROUP_ID</span> ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"good guy"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"bad guy"</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="不使用id命令-判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。"><a href="#不使用id命令-判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。" class="headerlink" title="(不使用id命令)判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。"></a>(不使用id命令)判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line">USER_UID=`grep <span class="string">"^<span class="variable">$1</span>:"</span> /etc/passwd | cut -d: -f3`</span><br><span class="line">GROUP_ID=`grep <span class="string">"^<span class="variable">$1</span>:"</span> /etc/passwd | cut -d: -f4`</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$USER_UID</span> -eq <span class="variable">$GROUP_ID</span> ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"good guy"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"bad guy"</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#	cut命令：字符串剪切</span></span><br><span class="line"><span class="comment">#		-d ：指定剪切的定位字符</span></span><br><span class="line"><span class="comment">#		-f ：指定剪切的字段数</span></span><br></pre></td></tr></table></figure>
<h4 id="如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；"><a href="#如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；" class="headerlink" title="如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；"></a>如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Auther: LiuGaoyong</span></span><br><span class="line"><span class="comment"># Date:   2018-04-15</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ ! -e <span class="variable">$1</span> ]; <span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"Can't dentified."</span></span><br><span class="line"><span class="keyword">elif</span> [ -f <span class="variable">$1</span> ]; <span class="keyword">then</span> </span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> is a file."</span></span><br><span class="line"><span class="keyword">elif</span> [ -d <span class="variable">$1</span> ]; <span class="keyword">then</span> </span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"<span class="variable">$1</span> is a directory."</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">"Can't dentified."</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>
<h4 id="传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。"><a href="#传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。" class="headerlink" title="传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。"></a>传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。</h4>
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Linux/" rel="tag"># Linux</a>
          
            <a href="/tags/Shell/" rel="tag"># Shell</a>
          
            <a href="/tags/bash/" rel="tag"># bash</a>
          
            <a href="/tags/Branch-Structure/" rel="tag"># Branch_Structure</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/grep/" rel="next" title="文本过滤命令 grep 与正则表达式基础">
                <i class="fa fa-chevron-left"></i> 文本过滤命令 grep 与正则表达式基础
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/sed_basic/" rel="prev" title="流编辑器 sed 的使用（基础篇）">
                流编辑器 sed 的使用（基础篇） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="http://p7b7this6.bkt.clouddn.com/18-6-3/47273717.jpg"
                alt="LiuGaoyong" />
            
              <p class="site-author-name" itemprop="name">LiuGaoyong</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">22</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">15</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">26</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiuGaoyong" target="_blank" title="GitHub"><i class="fa fa-fw fa-github"></i>GitHub</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://weibo.com/LiuGaoyongYoung" target="_blank" title="Weibo"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://twitter.com/liugaoyong" target="_blank" title="Twitter"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                  
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.facebook.com/liugaoyong" target="_blank" title="FaceBook"><i class="fa fa-fw fa-facebook"></i>FaceBook</a>
                  
                </span>
              
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.github.com" title="Github 官网" target="_blank">Github 官网</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Bash条件测试"><span class="nav-number">1.</span> <span class="nav-text">Bash条件测试</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#条件测试种类"><span class="nav-number">1.1.</span> <span class="nav-text">条件测试种类</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#整数测试（双目操作符）"><span class="nav-number">1.1.1.</span> <span class="nav-text">整数测试（双目操作符）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字符串测试（双目单目都有）"><span class="nav-number">1.1.2.</span> <span class="nav-text">字符串测试（双目单目都有）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#文件测试（单目操作符）"><span class="nav-number">1.1.3.</span> <span class="nav-text">文件测试（单目操作符）</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#条件测试表达式"><span class="nav-number">1.2.</span> <span class="nav-text">条件测试表达式</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算术运算的实现方式"><span class="nav-number">1.3.</span> <span class="nav-text">算术运算的实现方式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分支结构（if语句）"><span class="nav-number">2.</span> <span class="nav-text">分支结构（if语句）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#单分支的if语句"><span class="nav-number">2.1.</span> <span class="nav-text">单分支的if语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#双分支的if语句"><span class="nav-number">2.2.</span> <span class="nav-text">双分支的if语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#多分支的if语句"><span class="nav-number">2.3.</span> <span class="nav-text">多分支的if语句</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例练习"><span class="nav-number">3.</span> <span class="nav-text">实例练习</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；"><span class="nav-number">3.1.</span> <span class="nav-text">给定一个用户：如果其UID为0，就显示此为管理员；否则，就显示其为普通用户；</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；"><span class="nav-number">3.2.</span> <span class="nav-text">判断系统上是否有用户的默认shell为bash；如果有，就显示有多少个；否则，就显示没有这类用户；</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。"><span class="nav-number">3.3.</span> <span class="nav-text">给定一个文件，判断这个文件中是否有空白行；如果有，则显示其空白行数；否则，显示没有空白行。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。"><span class="nav-number">3.4.</span> <span class="nav-text">判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#不使用id命令-判断给定用户UID与GID是否一样。如果一样，就显示“good-guy”；否则，就显示“bad-guy”。"><span class="nav-number">3.5.</span> <span class="nav-text">(不使用id命令)判断给定用户UID与GID是否一样。如果一样，就显示“good guy”；否则，就显示“bad guy”。</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；"><span class="nav-number">3.6.</span> <span class="nav-text">如果是一个普通文件，就显示之；如果是一个目录，亦显示之；否则，此为无法识别之文件；</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。"><span class="nav-number">3.7.</span> <span class="nav-text">传递一个用户名参数给脚本，判断此用户的用户名跟其基本组的组名是否一致，并将结果显示出来。</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiuGaoyong</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/theme-next/hexo-theme-next">NexT.Gemini</a> v6.1.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.1.0"></script>



  



	





  





  










  





  

  

  

  

  
  

  

  

  

  

</body>
</html>
